﻿using BPMS.Business;
using BPMS.Entity;
using BPMS.IBusiness;
using DotNet.Utilities;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace BPMS.MVC.Controllers
{
    public class FlexibleFormController : BaseController
    {
        BPMS_ItemDetailsIBLL bpms_itemdetailsibll = new BPMS_ItemDetailsBLL();
        BPMS_AppendPropertyIBLL bpms_appendpropertyibll = new BPMS_AppendPropertyBLL();
        BPMS_AppendProperty bpms_appendproperty = new BPMS_AppendProperty();

        #region 自定义表单管理
        /// <summary>
        /// 自定义表单管理
        /// </summary>
        /// <returns></returns>
        public ActionResult FlexibleFormIndex()
        {
            return View();
        }
        #endregion

        #region 自定义表单列表
        /// <summary>
        /// 自定义表单列表
        /// </summary>
        /// <returns></returns>
        public ActionResult FlexibleFormList()
        {
            return View();
        }
        /// <summary>
        /// 处理数据
        /// </summary>
        public void LoadAction()
        {
            string active = Request["action"];                                          //提交类型
            string orderField = Request["pqGrid_OrderField"];                           //排序字段  
            string orderType = Request["pqGrid_OrderType"];                             //排序类型
            string pqGrid_Sort = Request["pqGrid_Sort"];                                //要显示字段
            string key = Request["key"];                                                //主键
            string ParentId = Request["ParentId"];                                      //主键
            switch (active)
            {
                case "GridBindList"://加载列表
                    Response.Write(JsonHelper.PqGridJson<BPMS_AppendProperty>(bpms_appendpropertyibll.GetList(ParentId), pqGrid_Sort));
                    Response.End();
                    break;
                case "Delete":    //删除数据
                    Response.Write(bpms_appendpropertyibll.Delete(key));
                    Response.End();
                    break;
                default:
                    break;
            }
        }
        #endregion

        #region 自定义表单信息
        /// <summary>
        /// 自定义表单信息
        /// </summary>
        /// <returns></returns>
        public ActionResult FlexibleFormForm()
        {
            return View();
        }
        /// <summary>
        /// 确认事件
        /// </summary>
        /// <param name="bpms_appendproperty">实体类</param>
        /// <returns></returns>
        public ActionResult AcceptClick(BPMS_AppendProperty bpms_appendproperty)
        {
            string key = Request["key"];                                                //主键
            bool IsOk = false;
            if (!string.IsNullOrEmpty(key))//判断是否编辑
            {
                bpms_appendproperty.PropertyId = key;
                bpms_appendproperty.ModifyDate = DateTime.Now;
                bpms_appendproperty.ModifyUserId = SessionHelper.GetSessionUser().UserId;
                bpms_appendproperty.ModifyUserName = SessionHelper.GetSessionUser().UserName;
                IsOk = bpms_appendpropertyibll.Update(bpms_appendproperty);
            }
            else
            {
                bpms_appendproperty.PropertyId = CommonHelper.GetGuid;
                bpms_appendproperty.CreateUserId = SessionHelper.GetSessionUser().UserId;
                bpms_appendproperty.CreateUserName = SessionHelper.GetSessionUser().UserName;
                IsOk = bpms_appendpropertyibll.Insert(bpms_appendproperty);
            }
            return Content(IsOk.ToString().ToLower());
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        /// <param name="key">主键</param>
        /// <returns></returns>
        public ActionResult InitControl(string key)
        {
            if (!string.IsNullOrEmpty(key))
            {
                bpms_appendproperty = bpms_appendpropertyibll.GetEntity(key);
                return Content(JsonHelper.ObjectToJson(bpms_appendproperty));
            }
            return null;
        }
        #endregion 

        #region 业务类别
        /// <summary>
        /// 业务类别
        /// </summary>
        /// <returns></returns>
        public ActionResult FlexibleFormType()
        {
            ViewData["strHtmlFormType"] = GetTree_FormType();
            return View();
        }
        /// <summary>
        /// 加载类别
        /// </summary>
        public string GetTree_FormType()
        {
            StringBuilder strHtml = new StringBuilder();
            IList list = bpms_itemdetailsibll.GetListByItemsId("4d0abda4-9059-4f45-946a-8a237bc606c1");//ID是固定的，
            int index = 0;
            string strclass = "";
            foreach (BPMS_ItemDetails entity in list)
            {
                if (index == 0)
                    strclass = "collapsableselected";
                else
                    strclass = "";
                strHtml.Append("<li>");
                strHtml.Append("<div class='" + strclass + "' id='" + entity.ItemCode + "'><img src='/Themes/images/16/award_star_silver_3.png' style='vertical-align: middle;' alt=''/><span>" + entity.ItemName + "</span></div>");
                strHtml.Append("</li>");
                index++;
            }
            return strHtml.ToString();
        }
        #endregion
    }
}
